<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="head.jsp"%>
<%
	String id = request.getParameter("id");
	id = id == null ? "create" : id;
	
	String projectId = request.getParameter("projectId");
%>
<input type="hidden" id="id_id" value="<%=id%>">


<div class="am-cf admin-main">
	<!-- content start -->
	<div class="admin-content">
		<div class="admin-content-body">
			<div class="am-cf am-padding am-padding-bottom-0">
				<div class="am-fl am-cf">
					<strong class="am-text-primary am-text-lg">构建项目</strong> / <small>选择表</small>
				</div>
			</div>
			<hr>

			<div class="am-g">
				<div class="am-u-sm-12 ">
					<form class="am-form " data-am-validator>
						<div class="am-form-group am-fr">
							<button type="button" id="id_project_back" class="am-btn am-btn-primary">上一步</button>
							<button type="button" id="id_project_save" class="am-btn am-btn-primary">保存修改</button>
							<button type="button" id="id_project_next" class="am-btn am-btn-primary">下一步</button>
						</div>
						<table class="am-table am-table-striped am-table-hover table-main">
							<thead>
								<tr>
									<th class="table-author am-hide-sm-only">选择</th>
									<th class="table-title">表名称</th>
									<th class="table-title">生成范围</th>
									<th class="table-title">对象名称</th>
									<th class="table-title">资源名称</th>
									<th class="table-title">是否树形</th>
									<th class="table-title">列表按钮</th>
								</tr>
							</thead>
							<tbody id="id_tb_body">
							</tbody>
						</table>
						<hr>
						<p>说明：请确保该系统所在机器能访问你的数据库</p>

					</form>
				</div>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">

function loadSaveData(){
	var data_arr;
	var id=$("#id_id").val();
	$.ajax({
        url:'mvc/table/getSavelist',
			type : "POST",
			data : {			
 				"bizId" : id
			},
			async : false,  
 			success : function(data){
 				data_arr=data.data;				
 			},
			error : function() {
				alert(data.message);
			}
		});
	return data_arr;
}

	$(function() {
		loadData();
		$("#id_project_save").on("click", function() {
			save();
		});

		$("#id_project_next").on("click", function() {
			window.location.href = "tablesselect.jsp?id=" + $("#id_id").val()+ '&projectId=' + '<%=projectId%>';
		});
		$("#id_project_back").on("click", function() {
			window.location.href = "prjconfig.jsp?id=" + $("#id_id").val();
		});
	});
	
	
	
	
	function loadData() {
		$.get("mvc/table/list?bizId=" + $("#id_id").val(), function(data) {
			if (data && data.success) {
				filltr(data.data);
				$('select').selected();
			} else {
				alert(data.message);
			}
		});
	}

	function save() {
		if ($("input[type=checkbox]:checked").length < 1)
			return;
		var arr = '[';
		$("input[type=checkbox]:checked").each(
				function() {
					arr += "{" + "'tableName' : '" + $(this).val() + "',"
							+ "'bizId' : " + $("#id_id").val() + ","
							+ "'menuname' : " + "'"+ ($("#id_res_" + $(this).val()).val() || '') + "',"
					      + "'objectName' : " + "'"+  ($("#id_obj_" + $(this).val()).val() || '')+"',"
					                    +"'isTree' : " + "'"+  ($("#select_istree_" + $(this).val()).val() ||'')+"',"
					      		 +"'listButton' : " + "'"+   ($("#select_listbutton_"+$(this).val()).val() ||'')+"',"
					      		 +"'generation' : " + "'"+   ($("#select_generation_"+$(this).val()).val() ||'')
							+ "'}";
					arr += ","
				});
		arr = arr.substr(0, arr.length - 1) + ']';
		$.post("mvc/table/addall", {
			'key' : arr
		}, function(data) {
			alert(data);
		});
	}

	function filltr(data_list) {
		data_list = data_list || [];
		$("#id_tb_body").html("");
		console.log(data_list);
		for (var idx in data_list) {
			var context = "";
			var prj = data_list[idx].tableName;
			var isTree=data_list[idx].isTree;
			var listButton=data_list[idx].listButton || '', listButton = listButton.split(",");
			var generation=data_list[idx].generation || '', generation = generation.split(",");
			var generation_service="";
			var listButton_curd="";
			if(generation==""){
		    	generation_service='<option value="dao" selected>持久</option>';
			}else{
				generation_service='<option value="dao"  '      +(generation.indexOf('dao')>-1 ? "selected='selected'" : '' ) + '>持久</option>';
			}
			if(listButton==""){
				listButton_curd= '<option value="search" selected>查询</option>'
			   						      + '<option value="add"     selected >新增</option>'
			 							  + '<option value="edit"     selected >修改</option>'
			    						  + '<option value="delete" selected>逻辑删除</option>';
			}else{
				listButton_curd= '<option value="search"  ' + (listButton.indexOf('search')>-1 ? "selected='selected'" : '' ) + '>查询</option>'
				      					  + '<option value="add"  ' + (listButton.indexOf('add')>-1? "selected='selected'" : '' ) + '>新增</option>'
									      + '<option value="edit"  ' + (listButton.indexOf('edit') >-1? "selected='selected'" : '' ) + '>修改</option>'
										  + '<option value="delete"  ' + (listButton.indexOf('delete')>-1 ? "selected='selected'" : '' ) + '>逻辑删除</option>';
			}
		/* 	if(data[idx].fieldType==""&&columnName.indexOf("desc")>-1||columnName.indexOf("content")>-1){
				textarea= "<option value='textarea' selected>textarea</option>"
			}else{
				textarea= "<option value='textarea' " + ( getStr(prj.fieldType) == 'textarea' ? 'selected' : '' ) + ">textarea</option>"			
			} */
			var checked_str='';
			if(data_list[idx].check){
				checked_str="checked";
			}

			context += "<td>" + ' <label class="am-checkbox"><input type="checkbox" name="cbx" value="' + prj + '"'+checked_str+'> </label></td>'+				
				"<td>"
					+ prj
					+ "</td>"
					+ '<td><select  id="select_generation_' + prj+'"  data-am-selected multiple>'
					+ '<option value="html" '     +(generation.indexOf('html')>-1 ? "selected='selected'" : '' ) + '>前端</option>'			
				    + '<option value="api" '        +(generation.indexOf('api')>-1 ? "selected='selected'" : '' ) + '>接口</option>'
				    + '<option value="service" '  +(generation.indexOf('service')>-1 ? "selected='selected'" : '' ) + '>业务</option>'
				    + generation_service
					+ '</select></td>'
					+ '<td><div class="am-u-sm-9">'
					+ '<input type="text" id="id_obj_' + prj +  '" '  
					+	'value="'+data_list[idx].objectName+'" ></div>'
					+ '</td><td>'
					+ '<input type="text"  id="id_res_' + prj +  '"' +'value="'+data_list[idx].menuName+'" ></div>'
					+ '</td>'			
					+ '<td><select  id="select_istree_' + prj+'"  data-am-selected>'
					+ '<option value="0"  ' + (isTree == '0' ? "selected='selected'" : '' ) + '>否</option>'			
				    + '<option value="1"  ' + (isTree == '1' ? "selected='selected'" : '' ) + '>是</option>'
					+ '</select></td>'
					+'<td><select  id="select_listbutton_'+prj+'"  data-am-selected  multiple>'
				    +listButton_curd
				    +'<option value="remove"  ' + (listButton.indexOf('remove')>-1 ? "selected='selected'" : '' ) + '>物理删除</option>'
				    + '<option value="import"  ' + (listButton.indexOf('import')>-1 ? "selected='selected'" : '' ) + '>导入</option>'
				    + '<option value="export"  ' + (listButton.indexOf('export')>-1? "selected='selected'" : '' ) + '>导出</option>'
					+'</select></td>'
			context = "<tr>" + context + "</tr>"; 
   
			
			$("#id_tb_body").append(context);
		}

	}
</script>

<%@include file="bottom.jsp"%>